package threadtest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @author shentianyu
 * @date 2021-06-29 13:39
 */
public class StudentExam implements Runnable {

    private long time;

    private CountDownLatch countDownLatch;

    private Semaphore semaphore;

    public StudentExam(long time, CountDownLatch countDownLatch, Semaphore semaphore) {
        this.time = time;
        this.countDownLatch = countDownLatch;
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"start");
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"finish");
        semaphore.release();
        countDownLatch.countDown();
    }
}
